<?php
/**
 *   This file is part of GeoArticle (a Joomla plugin).
 *   Written by Ivar Bergman (www.ivarbergman.se)
 *   Copyright (c) 2010 Ivar Bergman. All rights reserved.
 *
 *   GeoArticle is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   GeoArticle is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with GeoArticle.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

/**
 * @package		
 * @subpackage	
 */
class GeoArticleModelGeoArticle extends JModel
{
  /**
   * Category ata array
   *
   * @var array
   */
  var $_data = null;

  /**
   * Category total
   *
   * @var integer
   */
  var $_total = null;

  /**
   * Pagination object
   *
   * @var object
   */
  var $_pagination = null;

  /**
   * Constructor
   *
   * @since 1.5
   */
  function __construct() {
    parent::__construct();

    global $mainframe, $option;

    // Get the pagination request variables
    $limit    = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
    $limitstart = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );

    // In case limit has been changed, adjust limitstart accordingly
    $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

    $this->setState('limit', $limit);
    $this->setState('limitstart', $limitstart);
  }

  /**
   * Method to get item data
   *
   * @access public
   * @return array
   */
  function getData() {
    // Lets load the content if it doesn't already exist
    if (empty($this->_data))
      {
	$query = $this->_buildQuery();
	$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
      }

    return $this->_data;
  }

  /**
   * Method to get the total number of items
   *
   * @access public
   * @return integer
   */
  function getTotal() {
    // Lets load the content if it doesn't already exist
    if (empty($this->_total))
      {
	$query = $this->_buildQuery();
	$this->_total = $this->_getListCount($query);
      }

    return $this->_total;
  }

  /**
   * Method to get a pagination object
   *
   * @access public
   * @return integer
   */
  function getPagination() {
    // Lets load the content if it doesn't already exist
    if (empty($this->_pagination))
      {
	jimport('joomla.html.pagination');
	$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
      }

    return $this->_pagination;
  }

  function _buildQuery() {
    // Get the WHERE and ORDER BY clauses for the query
    $where   = $this->_buildContentWhere();
    $orderby = $this->_buildContentOrderBy();
    
    $query = sprintf(
		     'SELECT ga.*,  u.name AS editor, c.name AS author FROM #__geoarticle AS ga'.
		     ' LEFT JOIN #__users AS u ON u.id = ga.checked_out'.
		     ' LEFT JOIN #__users AS c ON c.id = ga.created_by'.
		     ' %s %s',
		     $where,
		     $orderby
		     );

    return $query;
  }

  function _buildContentOrderBy() {
    global $mainframe, $option;

    $filter_order     = $mainframe->getUserStateFromRequest( $option.'filter_order',    'filter_order',   'ordering',   'cmd' );
    $filter_order_Dir = $mainframe->getUserStateFromRequest( $option.'filter_order_Dir',  'filter_order_Dir', 'asc',       'word' );

    $orderby  = sprintf(' ORDER BY %s %s, ordering', $filter_order, $filter_order_Dir );

    return $orderby;
  }

  function _buildContentWhere() {
    global $mainframe, $option;
    $db         =& JFactory::getDBO();
    $filter_state     = $mainframe->getUserStateFromRequest( $option.'filter_state',     'filter_state',     '',      'word' );
    $filter_catid     = $mainframe->getUserStateFromRequest( $option.'filter_catid',     'filter_catid',     0,       'int' );
    $filter_order     = $mainframe->getUserStateFromRequest( $option.'filter_order',     'filter_order',     'title', 'cmd' );
    $filter_order_Dir = $mainframe->getUserStateFromRequest( $option.'filter_order_Dir', 'filter_order_Dir', 'asc',   'word' );
    $search           = $mainframe->getUserStateFromRequest( $option.'search',           'search',           '',      'string' );
    $search           = JString::strtolower( $search );

    $where = array();

    if ($search) {
      $where[] = "LOWER(CONCAT(ga.title, ga.description)) LIKE ".$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
    }
    if ( $filter_state ) {
      if ( $filter_state == 'P' ) {
	$where[] = 'ga.state = 1';
      } else if ($filter_state == 'U' ) {
	$where[] = 'ga.state = 0';
      }
    }
    
    $where = ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );
    return $where;
  }

  function &_getTable() {
    if ($this->_table == null) {
      $this->_table =& JTable::getInstance( 'geoarticle');
    }
    return $this->_table;
  }
}
